/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

/**
 *
 * @author David
 */
import java.util.*;
import java.io.*;
import AirLine.*;
import Database.DBObject;
import edu.chl.chalmers.fatclient.sb.Flight;
import java.sql.*;
import java.math.*;

public class RandomDataCreatorOskarDavid {

    private RandomDataCreator rdc;
    /*
    private Set<Airport> airportList;
    private Set<Flight> flightList;
    private ArrayList<Airplane> planeList;
    private ArrayList<Airport> forFlights;
    
    private int nrFlights, nrRows, nrColumns, nrPlanes;
    String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    //private String[] alpha;
     * */
    public static void main(String[] args) {
        new RandomDataCreatorOskarDavid();

    }

    public RandomDataCreatorOskarDavid() {
        
        /*
        airportList = new TreeSet<Airport>();
        forFlights = new ArrayList<Airport>();
        flightList = new TreeSet<Flight>();
        planeList = new ArrayList<Airplane>();
        nrFlights = 10;
        nrPlanes = 10;
        nrRows = 5;
        nrColumns = 5;
        getAirports();
        getPlanes();
        getFlights();
        */
        
        //System.out.println(Math.random());
        // addAirports();
       RandomDataCreator.initialize();
       
       if(false)
           createDummyBooking();
       
       if(true)
       {
           if(true)
           {
                addAirports();
                addAirline();
           }

        addPlanes();
        addSeats();
        addFlights();

       }
       else
       {
           removeStuff();
           createDummyBooking();
       }
    }

    private void createDummyBooking()
    {
         DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("INSERT INTO Booking (flightRow, flightColumn, planeID, flightID, departureTime, passengerSSN) values (?,?,?,?,?,?)");
            
            stm.setInt(1, 1);
            stm.setInt(2,1);
            stm.setString(3,RandomDataCreator.getRandomLetters(5));        
            stm.setString(4,RandomDataCreator.getRandomLetters(5));        
            stm.setTimestamp(5, RandomDataCreator.getTimes()[0]);
            stm.setInt(6, 851122);
            
            
            stm.execute();
            mm.commit();
            
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }
    
    private void removeStuff()
    {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("DELETE FROM Seat");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("DELETE FROM Airline");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("DELETE FROM Airport");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("DELETE FROM Flight");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("DELETE FROM Airplane");
            stm.execute();
            mm.commit();
            
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addAirline()
    {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("INSERT INTO Airline (name, ID, discount) values(?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            
                try {
                    stm.setString(1, "We fly - You Scream");
                    stm.setString(2, ""+1);
                    stm.setInt(3, 10);
                    stm.execute();
                    mm.commit();
                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i första..." + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }
                }
            
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addAirports() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("INSERT INTO Airport (name, ID) values(?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airport a : RandomDataCreator.getAirports()) {
                try {
                    stm.setString(1, a.getAirportName());
                    stm.setString(2, a.getAirportCode());
                    stm.execute();
                    mm.commit();
                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i första..." + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }
                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addPlanes()
    {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("INSERT INTO Airplane (ID) values(?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airplane p : RandomDataCreator.getAirplanes()) {
               
                    try {

                        stm.setString(1, p.getId());
                        stm.execute();
                        mm.commit();
                    } catch (SQLException e) {
                        try {
                            mm.rollBack();
                            System.out.println("fel i addplanes..." + e.getLocalizedMessage());
                        } catch (Exception se) {
                            System.out.println("feeel");
                        }
                    }
  
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }
            
    
    private void addSeats() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("INSERT INTO Seat (planeID, flightRow, flightColumn, seatType) values(?,?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airplane p : rdc.getAirplanes()) {
                for (Seat s : p.getSeats()) {
                    try {
                        
                        //System.out.println("planet vi försöker med: "+p.getId());
                        stm.setString(1,  p.getId());
                        
                        stm.setInt(2, s.getRow());
                        stm.setInt(3, s.getColumn());
                        stm.setInt(4, s.getType());

                        stm.execute();
                        mm.commit();
                    } catch (SQLException e) {
                        try {
                            mm.rollBack();
                            System.out.println("fel i addseats..." + e.getLocalizedMessage());
                        } catch (Exception se) {
                            System.out.println("feeel");
                        }
                    }
                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addFlights() {
        DBObject mm = new DBObject();
        PreparedStatement stm;

        try {
            mm.connect();
            stm = mm.getConnection(DBObject.MYSQL_013).prepareStatement("INSERT INTO Flight (ID, departureTime, arrivalTime, departure, destination, plane, price) values(?,?,?,?,?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Flight f : rdc.getFlights()) {
                try {
                    stm.setString(1, f.getCode());
                    stm.setTimestamp(2, f.getDepartureTime());
                    stm.setTimestamp(3, f.getArrivalTime());
                    stm.setString(4, f.getDepartureAirport().getAirportCode());
                    stm.setString(5, f.getDestinationAirport().getAirportCode());
                    stm.setString(6, f.getPlaneId());
                    stm.setInt(7, f.getPrice());
                    // stm.setString(2, a.getAirportCode());
                    stm.execute();
                    mm.commit();
                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i addflights" + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }

                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i första..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }

        }
    }

}
